package com.paireach.szwolaiyun.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.util.Log;

import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.activity.Activity_B;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;

public class JPushReceiver extends BroadcastReceiver {

    private static final String TAG = "0012";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            // 收到自定义推送消息，通过自定义消息来判断如何做跳转
            Log.e(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//            String tmp1=bundle.getString(JPushInterface.EXTRA_TITLE);
            String extraJson = bundle.getString(JPushInterface.EXTRA_EXTRA);
            String orderNo;
            int statusCode;
            String dcDeptType;
            try {
                JSONObject object = new JSONObject(extraJson);
                String str = object.getString("params");
                JSONObject jsonObject = new JSONObject(str);

                Iterator<String> keyIter = jsonObject.keys();
                String key;
                String value;
                Map<String, String> valueMap = new HashMap<String, String>();
                while (keyIter.hasNext()) {
                    key = keyIter.next();
                    value = jsonObject.getString(key);
                    valueMap.put(key, value);
                }
                try{
                    orderNo = valueMap.get("orderCode");
                }catch (Exception e){
                    orderNo = "";
                }
                try{
                    statusCode = Integer.parseInt(valueMap.get("statusCode"));
                }catch (Exception e){
                    statusCode=0;
                }
                try{
                    dcDeptType = valueMap.get("dcDeptType");
                }catch (Exception e){
                    dcDeptType="";
                }
            } catch (Exception e) {
                orderNo = "";
                statusCode=0;
                dcDeptType="";
            }
            Intent intent1 = new Intent(context, Activity_B.class);
            intent1.putExtra("orderNumber", orderNo);
            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
            showNotification(context, message, pendingIntent);

            //在这里做了一个静态广播转动态广播的操作，只需要在要处理的界面上注册一个动态广播，然后实现界面跳转
            //发送两条广播，一条sticky，一条非sticky
            Intent broadcastJump = new Intent();
            broadcastJump.setAction("com.paireach.szwolaiyun.ACTION_JUMP");
            Bundle bundle1 = new Bundle();
            bundle1.putString("orderCode",orderNo);
            bundle1.putInt("status",statusCode);
            bundle1.putString("dcDeptType",dcDeptType);
            broadcastJump.putExtras(bundle1);
            context.sendStickyBroadcast(broadcastJump);

            Intent nonSticky=new Intent("com.paireach.szwolaiyun.ACTION_JUMP_NON_STICKY");
            Bundle bundle2 = new Bundle();
            bundle2.putString("orderCode",orderNo);
            nonSticky.putExtras(bundle2);
            context.sendBroadcast(nonSticky);
        }
    }

    private void showNotification(Context context, String message, PendingIntent pendingIntent) {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentTitle("我来运");
        builder.setContentText(message);
        builder.setSmallIcon(R.mipmap.tts_logo1);
        builder.setAutoCancel(true);
        builder.setContentIntent(pendingIntent);
        Notification notification = builder.build();
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        nm.notify(0, notification);
    }

}
